package com.iss.demo.controller;

import com.iss.demo.config.PrereadConfig;
import com.iss.demo.entity.Car;
import com.iss.demo.model.MyResponseData;
import com.iss.demo.service.CarService;
import com.iss.demo.model.CarModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("car/")
public class CarController {
    @Autowired
    private CarService service;

    @RequestMapping("selectAll")
    public List<CarModel> selectAll(){
        List<CarModel> list=service.selectAll();
        return list;
    }

    @RequestMapping("selectInventory")
    public List<CarModel> selectInventory(){
        List<CarModel> list=service.selectInventory();
        return list;
    }

    @RequestMapping("selectAllByType")
    public List<CarModel> selectAllByType(String t_name){
        List<CarModel> list=service.selectAllByType(t_name);

        return list;
    }

    @RequestMapping("selectAllByBrand")
    public MyResponseData selectAllByBrand(String b_name){
        List<CarModel> list=service.selectAllByBrand(b_name);
        MyResponseData responseData = new MyResponseData();
        responseData.setStatus("success");
        responseData.setErrorCode(0);
        responseData.setObjectList(list);
        return  responseData;
    }

    @RequestMapping("selectAllByName")
    public CarModel selectAllByName(String c_name){
        CarModel carModel=service.selectAllByName(c_name);
        return  carModel;
    }

    @RequestMapping("selectAllById")
    public CarModel selectAllById(Integer c_id){
        CarModel carModel=service.selectAllById(c_id);
        return carModel;
    }
    @RequestMapping("selectAllByOther")
    public List<CarModel> selectAllByOther(String t_name,String b_name,Integer c_min_price,Integer c_max_price){
        List<CarModel> list=service.selectAllByOther(t_name,b_name,c_min_price,c_max_price);
        return list;
    }

    @RequestMapping("getInfoById")
    public MyResponseData getInfoById(Integer c_id,HttpServletRequest request){
        MyResponseData responseData=new MyResponseData();
        //从相应的文件夹读取图片
        File path=new File(config.getUploadPath()+File.separator+c_id);
        File[] files=path.listFiles();
        String[] paths=new String[files.length];
        for(int i=0;i<files.length;i++){
            paths[i]="uploadFile"+File.separator+c_id + File.separator + files[i].getName();

        }
        responseData.setObject(paths);
        CarModel carModel =service.selectAllById(c_id);
        Map<String, Object> map = new HashMap<>();
        map.put("car", carModel);
        responseData.setObjectMap(map);
        return responseData;
    }

    @RequestMapping("insertCar")
    public MyResponseData insertCar(Car car){
        Integer count=service.insertCar(car);
        MyResponseData responseData = new MyResponseData();
        responseData.setErrorCode(0);
        responseData.setStatus("添加了"+count+"款汽车");
        return responseData;
    }

    @RequestMapping("insertInventory")
    public MyResponseData insertInventory(Car car){
        Integer count=service.insertInventory(car);
        MyResponseData responseData = new MyResponseData();
        responseData.setErrorCode(0);
        responseData.setStatus("库存已更新");
        return responseData;
    }

    @RequestMapping("updateCar")
    public MyResponseData updateCar(Car car){
        Integer count=service.updateCar(car);
        MyResponseData responseData = new MyResponseData();
        responseData.setErrorCode(0);
        responseData.setStatus("更新了"+count+"款汽车");
        return responseData;
    }

    @RequestMapping("deleteInventory")
    public MyResponseData deleteInventory(Integer in_id){
        Integer count = service.deleteInventory(in_id);
        MyResponseData responseData = new MyResponseData();
        responseData.setErrorCode(0);
        responseData.setStatus("删除成功");
        return responseData;
    }

    @RequestMapping("selectById")
    public MyResponseData selectById(Integer id){
        Car car=service.selectById(id);
        MyResponseData responseData=new MyResponseData();
        responseData.setErrorCode(0);
        responseData.setStatus("success");
        responseData.setObject(car);
        return responseData;
    }

    @RequestMapping("delete")
    public MyResponseData delete(Integer[] c_ids){
        Integer count = service.delete(c_ids);
        MyResponseData responseData = new MyResponseData();
        responseData.setErrorCode(0);
        responseData.setStatus("成功删除："+count+"款");
        return responseData;
    }

    @Autowired
    PrereadConfig config;

    @RequestMapping("upload")
    public MyResponseData upload(@RequestParam(value = "files", required = false) MultipartFile[] files, Integer c_id,  HttpServletRequest request) {
        MyResponseData responseData = new MyResponseData();
        if (files == null || files.length == 0) {
            responseData.setErrorCode(-1);
            responseData.setStatus("没有选择文件");
        }

        File path = new File(config.getUploadPath()+File.separator+c_id);
        if (!path.exists()) {
            path.mkdirs();
        }

        String surfaceName = "";
        for (int i = 0; i < files.length; i++) {
            String newFileName = System.currentTimeMillis() + "_" + files[i].getOriginalFilename();
            File newFile = new File(path, newFileName);   //保存文件
            if (i == 0) {
                surfaceName = "uploadFile"+File.separator+c_id+File.separator+newFileName;
            }
            try {
                FileOutputStream fos = new FileOutputStream(newFile);
                InputStream is = files[i].getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                is.close();
                responseData.setErrorCode(0);
                responseData.setStatus("上传成功");
                Integer count = service.updateCarInfo(surfaceName, c_id);
            } catch (IOException e) {
                responseData.setErrorCode(-2);
                responseData.setStatus("上传失败");
                e.printStackTrace();
            }
        }
        return responseData;
    }


}
